<?php
/**
 * 后台节点(权限控制节点)模块
 * 
 */
class MenuAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();	//RBAC 验证接口初始化
		parent::_initmenu();	
		Vendor('Common.Tree');	//导入通用树型类
	}
	
	//列表
	public function index(){
		
		/* $node = D("Node");
		$list = $node->where("level=2")->select();
		foreach($list as $v){
			$parent = $node->find($v['pid']);
			$node->save(array("id"=>$v['id'],"module"=>$parent['name'],'data'=>$parent['name']."/".$v['name']));
		}
		exit(); */
		
		$menu = D('Menu')->getAllNode();
		//F("Menu_All",$menu);
		$array = array();
		// 构建生成树中所需的数据
		$childAdd = L('nod_add_child_title');
		$nodeModi = L('cfg_list_itf_btn_modify');
		$nodeDele = L('cfg_list_itf_btn_delete');
		$conDelet = L('nod_oth_dtis_delCon');
		$lang     = getLang();
		foreach($menu as $k => $r) {
			$r['title']   = dejsonByLang($r['value'],$lang);
			$r['status']  = $r['status']==1 ? '<font color="red">√</font>' :'<font color="blue">×</font>';
			
			//todo level
			$sub          = ($r['level']>1) ? 'href="#" class="btn"' : 'href="'.U("/Menu/add/id/".$r['id']).'" class="btn blue"';
			
			$r['submenu'] = '<a '.$sub.' title="'.$childAdd.'" ><i class="icon-plus"></i></a>';
			
			$etidurl      = U('Menu/edit','id='.$r['id'].'&pid='.$r['pid']);
			$edit         = $r['level']==0 ? 'href="#" class="btn"' : 'href="'.$etidurl.'" class="btn blue"';
			$r['edit']    = '<a '.$edit.' title="'.$nodeModi.'" ><i class="icon-edit"></i></a>';
			
			$del          = $r['haveLeaf']==1 ? 'href="" class="btn"' : 'href="'.U('/Menu/del/id/'.$r['id']).'" class="btn blue"'; 
			$r['del']     = '<a '.$del.'  title="'.$nodeDele.'" ><i class="icon-remove"></i></a>';
			
			$r['levels']   = $r['level'];
			$r['url']      = $r['url'];
			$array[]       = $r;
		}

		$str  = "<tr class='tr level_\$levels' leaf='\$haveLeaf' level='\$levels'>
				    <td align='center'><input style='width:55px' type='text' value='\$sort' size='3' name='sort[\$id]'></td>
				    <td class='name'>\$spacer \$title</td> 
				    <td align='center'>\$url</td> 
				    <td align='center'>\$status</td> 
					<td align='center'>
						\$submenu \$edit \$del
					</td>
				  </tr>";

  		$Tree = new Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		$Tree->init($array);
		$html_tree = $Tree->get_tree(0, $str);

		$this->assign('html_tree',$html_tree);
		$this->display();
	}

	//添加
	public function add(){
		$menu   = D("Menu");
		$lanArr = C("LANG_ARRAY");
		$lang   = getLang();
		if(IS_POST) {
			if($menu->create()){
				$values      = $this->_post("value");
				$menu->value = json_encode($values);
				$menu->title = $values[$lang];
				$pid         = $this->_post("pid","intval",0);
				
				$id = $menu->add();
				$menu->genMenu($pid,$id);
				wlog(1,"",$id);
				
				$this->assign("jumpUrl",U('Menu/index'));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$menu->getError());
				$this->error($menu->getError());
			}	
		}else{
			$id = $this->_get("id","intval",0);
			if($id){
				$row  = $menu->find($id);
				$this->assign("row",$row);
				
				$array = $menu->createNodeSelect(0);
				$str   = "<option value='\$id' url='\$url' \$disabled >\$spacer \$title</option>";
				$Tree  = new Tree();
				$Tree->init($array);
				$select= $Tree->get_tree(0, $str, 0);
				$this->assign('node',$select);
			}
			$this->assign("lang",$lanArr);
			$this->display();
		}
	}

	//编辑
	public function edit(){
		
		$menu  = D('Menu');
		$lang  = getLang();
		$id    = $this->_param("id","intval",0);
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		if(IS_POST) {
			if($menu->create()){
				$values      = $this->_post("value");
				$menu->value = json_encode($values);
				$menu->title = $values[$lang];
				$pid         = $this->_post("pid","intval",0);
				
				$menu->save();
				wlog(1,"",$id);
				$this->assign("jumpUrl",U('Menu/index'));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$menu->getError());
				$this->error($menu->getError());
			}
		}else{
			$row = $menu->find($id);
			$this->assign("row",$row);
			
			if($row['pid']){
				$array = $menu->createNodeSelect($row['nodeid']);
				$str   = "<option value='\$id' \$selected url='\$url' \$disabled >\$spacer \$title</option>";
				$Tree  = new Tree();
				$Tree->init($array);
				$select= $Tree->get_tree(0, $str,$row['nodeid']);
				$this->assign('node',$select);
			}
			
			$value = json_decode($row['value'],true);
			$lang  = C("LANG_ARRAY");
			$vals  = array();
			foreach($lang as $k=>$v){
				$title = $value[$k];
				$vals[$k] = array('v'=>$v,'title'=>$title);
			}
			$this->assign("lang",$vals);
			
			$this->display();
		}

	}
	
	//删除
	public function del(){
		$id = $this->_param('id','intval',0);
		if(!$id)$this->error(L('cfg_form_tipmsg_illgel'));
		
		$menu = D('Menu');
		$info = $menu -> getNode(array('id'=>$id),'id,pid');
		
		if($menu->childNode($info['id'])){
			$this->error(L('nod_oth_dtis_exChild'));
		}
		
		if($menu->delNode('id='.$id)){
			$menu->updLeaf($info["pid"],true);
			wlog(1,"",$id);
			
			$this->assign("jumpUrl",U('Menu/index'));
			$this->success(L('cfg_form_tipmsg_delsucc'));
		}else{
			wlog(2);
			$this->error(L('cfg_form_tipmsg_delfail'));
		}
	}

	//节点排序
	public function sort(){
		$sorts = $this->_POST('sort');
		if(!is_array($sorts))$this->error(L('cfg_form_tipmsg_illgel'));
		
		$model = D("Menu");
		
		foreach ($sorts as $id => $sort) {
			$model->save(array("id"=>$id,"sort"=>$sort));
		}
		$this->assign("jumpUrl",U('Menu/index'));
		$this->success(L('nod_oth_sort_succ'));
	}

	
}